package com.wqn;

import java.sql.*;

public class JDBCUtils {

            //mysql驱动包名
            private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
            //数据库连接地址
            private static final String URL = "jdbc:mysql://localhost:3306/test";
            //用户名
            private static final String USER_NAME = "root";
            //密码
            private static final String PASSWORD = "123456";


            // 获取数据库连接
 public static Connection getConnection(){
                try {
                    //加载mysql的驱动类
                    Class.forName(DRIVER_NAME);
            //获取数据库连接
            System.out.println("连接数据库成功");
            return DriverManager.getConnection(URL, USER_NAME, PASSWORD);

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    //关闭资源
    public static void close(ResultSet rs,PreparedStatement ps,Connection con){
        try {
            if(rs!=null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(ps!=null){
                ps.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(con!=null){
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }








}